using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Preferences;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using NorthStar.Passenger.Application;
using TheNorthStar.Api.Results;

namespace NorthStar.Passenger
{
    [Service]
    public class NorthStarBackgroundService : Service
    {
        private System.Timers.Timer timer;
        public override IBinder OnBind(Intent intent)
        {
            return null;
        }

        public override void OnCreate()
        {
            base.OnCreate();
            timer = new System.Timers.Timer(30000);
            timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);

        }

        void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            ThreadPool.QueueUserWorkItem(RefreshBooking);
        }
        public override void OnStart(Intent intent, int startId)
        {
            base.OnStart(intent, startId);

            timer.Start();
        }

        public override void OnDestroy()
        {
            base.OnDestroy();
            timer.Stop();
        }
        private void RefreshBooking(object data)
        {
            Booking booking = null;
            ISharedPreferences _preferences = PreferenceManager.GetDefaultSharedPreferences(ApplicationContext);
            string bookingJson = _preferences.GetString("lastBookingRef", string.Empty);
            if (bookingJson != string.Empty)
            {
                booking = Newtonsoft.Json.JsonConvert.DeserializeObject<Booking>(bookingJson);

                if (booking.Status != "FINISHED")
                {
                    //make async call to refresh
                    ApiService api = new ApiService(Helper.GetServer(ApplicationContext));
                    var result = api.GetBooking(booking.BookingId);
                    var newBooking = Newtonsoft.Json.JsonConvert.DeserializeObject<Booking>(result);

                    ISharedPreferencesEditor edit = _preferences.Edit();
                    edit.PutString("lastBookingRef", result);
                    edit.Commit();
                    string ns = Context.NotificationService;
                    NotificationManager notificationManager = NotificationManager.FromContext(ApplicationContext);
                    Notification notification = new Notification(Resource.Drawable.Icon, "NorthStar Taxi");
                    Intent notifyIntent = new Intent(this, typeof(MyBookingActivity));
                    PendingIntent contentIntent = PendingIntent.GetActivity(this, 0, notifyIntent, 0);
                   
                    if (booking.Status == "NEW" && newBooking.Status == "CONFIRMED")
                    {


                        notification.SetLatestEventInfo(ApplicationContext, "Northstar Taxi", string.Format("Booking {0} confirmed. Your driver {1} is on their way", newBooking.BookingId, newBooking.DriverName), contentIntent);
                        notificationManager.Notify(1, notification);

                        /* string ns = Context.NotificationService;
                         NotificationManager notificationManager = NotificationManager.FromContext(ApplicationContext);
                         Notification notification = new Notification(Resource.Drawable.Icon, "hi");
                         notificationManager.Notify(1,notification);*/
                        //send confirmation notification
                    }
                    else if (booking.Status == "CONFIRMED" && newBooking.Status == "FINISHED")
                    {
                        //send thankyou notification
                        notification.SetLatestEventInfo(ApplicationContext, "Northstar Taxi", string.Format("Hi, Thanks for using NorthStar Taxi.", newBooking.BookingId, newBooking.DriverName), contentIntent);
                        notificationManager.Notify(1, notification);
                    }
                    
                }
                else
                {
                    this.StopSelf();
                    
                }

            }
        }
    }
}